package com.soilflow.info.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.constant.LandConstants;
import com.soilflow.common.domain.DahuCarpooling;
import com.soilflow.common.domain.bigdata.LandBigdataDto;
import com.soilflow.common.domain.bigdata.LandCountGroupByStatus;
import com.soilflow.common.domain.bigdata.LandCountVO;
import com.soilflow.common.domain.bigdata.LandDataVO;
import com.soilflow.framework.web.domain.server.Sys;
import com.soilflow.info.mapper.BigdataMapper;
import com.soilflow.info.mapper.DahuCarpoolingMapper;
import com.soilflow.info.service.IBigdataService;
import com.soilflow.info.service.IDahuCarpoolingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author party
 * @date 2023-10-30
 */
@Service
public class BigdataServiceImpl extends ServiceImpl<BigdataMapper, LandBigdataDto> implements IBigdataService {

    @Resource
    private BigdataMapper bigdataMapper;

    // 查询部门的土地统计数据
    public LandCountVO countDeptLand(Long deptId){
        LandBigdataDto dto = new LandBigdataDto();
        dto.setDeptId(deptId);
        List<LandCountGroupByStatus> countData = this.countLandGroupByStatus(dto);

        LandCountVO res = new LandCountVO();
        for(int i=0; i<countData.size(); i++){
            // 全量土地
            res.setTotal( res.getTotal().add(countData.get(i).getTotal()) );
            System.err.println(res.getTotal().add(countData.get(i).getTotal()));
            // 已流转
            if(countData.get(i).getStatus().equals(LandConstants.LandStatus.CIRCULATED.getValue())){
                res.setCirculated(countData.get(i).getTotal());
            }
        }

        // 未流转
        dto.setUsage(LandConstants.LandUsage.RESERVED.getValue());
        BigDecimal reserved = this.selectAllLand(dto);
        reserved = reserved == null ? BigDecimal.ZERO : reserved;
        res.setReserved(reserved);

        // 全量土地 减 已流转土地 减 预留地 = 实际未流转土地，自留地原则不上参与流转
        BigDecimal uncirculated = res.getTotal().subtract(res.getUncirculated());
        uncirculated = uncirculated.subtract(reserved);
        res.setUncirculated(uncirculated);

        return res;
    }

    @Override
    public BigDecimal selectAllLand(LandBigdataDto dto) {
        return bigdataMapper.selectAllLand(dto);
    }

    @Override
    public List<LandCountGroupByStatus> countLandGroupByStatus(LandBigdataDto dto) {
        return bigdataMapper.countLandGroupByStatus(dto);
    }

    @Override
    public LandDataVO countLandData(LandBigdataDto dto) {
        return bigdataMapper.countLandData(dto);
    }

    @Override
    public List<LandDataVO> countLandDataGroupByMonth(LandBigdataDto dto){
        return bigdataMapper.countLandDataGroupByMonth(dto);
    }

}
